package com.kedacom.freedomLabel.utils.validator;

import android.content.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * @author licheng
 * @since 2020/2/2 12:14
 */
public class EditTextValidator {

    private List<ValidationModel> validationModels;
    private Context context;

    public EditTextValidator(Context context) {
        this.context = context;
        this.validationModels = new ArrayList<>();
    }

    public EditTextValidator add(ValidationModel validationModel) {
        validationModels.add(validationModel);
        return this;
    }

    public EditTextValidator add(int position, ValidationModel validationModel) {
        validationModels.add(position, validationModel);
        return this;
    }

    public void remove(int position) {
        if (position > -1 && position < validationModels.size()) {
            validationModels.remove(position);
        }
    }

    public int size() {
        return validationModels.size();
    }

    public EditTextValidator clear() {
        validationModels.clear();
        return this;
    }

    // 这一步则是根据需求进行判断 如果符合规则返回true 否则返回false
    public boolean validate() {
        for (ValidationModel validationModel : validationModels) {
            // 如果没有校验器处理，那么直接返回
            if (validationModel.getValidationExecutor() == null || validationModel.getTextView() == null) {
                return true;
            }
            // 如果某个校验不正确 立马返回错误
            if (!validationModel.getValidationExecutor().doValidate(context, validationModel.getTextView().getText().toString())) {
                return false;
            }
        }
        return true;
    }
}